package TUILayer;//makes this class a part of the TUILayer package
import java.util.*;
import ControlLayer.*;//The TUILayer needs to know the controllers in the ControlLayer
import ModelLayer.*;//The TUILayer needs to know different classes from the Modellayer to create instances

/**
 * Write a description of class LendingsUI here.
 * 
 * @author (Kristian Henriksen) 
 * @version (2012.11.14)
 */
public class LendingsUI
{
    private LendingsCtr lendingsCtr;//references to the ControlLayer
    private AddressBookUI addrUI;
    private DvdUI dvdUI;

    /**
     * Constructor for objects of class LendingsUI
     */
    public LendingsUI()
    {
       lendingsCtr = new LendingsCtr();
       addrUI = new AddressBookUI();
       dvdUI = new DvdUI();
    }
    
    /**
     * initiating method for the address menu.
     */
     public void start()
    {
        System.out.print("\f");
        lendingsMenu();
    }
    
    /**
     *  Returns the menu, from where the user can make a choise.
     */
    public void lendingsMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choise = writeLendingsMenu();
            if (choise == 1){
                showLoanInformation();
            }
            
            if(choise == 2){
               createLend();
            }
            
            if(choise == 3){
                listAllPersons();
            }
            
            if(choise == 4){
                listAllDVDs();
            }
             
            if(choise == 5){
                listAllLendings();
            }
            
            if(choise == 6){
                returnLending();
            }
  
            if(choise == 7){
                exit = true;
                System.out.print("\f");
            }
            else if(choise > 7){
                System.out.println("\fInvalid choise, please try again");
            }
        }//end else
    }//end else
    
    /**
     * prints range of choices and takes an input from user.
     */
    private int writeLendingsMenu()
    {
           // creates an object keyboard to read data from the keyboard
            int choise = 0;
            Scanner keyboard = new Scanner(System.in);
            System.out.println("  *** Lending Menu ***");
            System.out.println(" (1) Show information about a loan");
            System.out.println(" (2) Create a new loan");
            System.out.println(" (3) Show list of loaners");
            System.out.println(" (4) Show list of DVD's");
            System.out.println(" (5) show list of loans");
            System.out.println(" (6) return a loan");
            System.out.println(" (7) Return to main menu");
            System.out.print("\n Make your choise: ");

            try
            {
                choise = keyboard.nextInt();
            }
            catch(Exception e)
            {   
                lendingsMenu();
                System.out.println("\fIt has to be a number, please try again");        
            }
            return choise;
    }
    
        /**
     * takes a scanner input of id and verifies it as type long before passing it on.
     */
    public long inputID()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Loan Id: ");
        long id = 0;
        try{
            id = keyboard.nextLong();
        }
        catch (InputMismatchException e) {
            System.out.println("\f");
        }     
        return id;
    }
    
    /**
     * takes an input of id and searches lending containers for a match. Cancel option by typing 0 (zero)
     */
    public long getId()
    {
        long i = inputID();
        boolean exit = false;
        
        while(lendingsCtr.getLending(i) == null && !exit)
        {
            System.out.println(" \f id not found, try again. Type 0 (zero) to exit.");
            i = inputID();
            if (i == 0)
                {
                exit = true;
                System.out.print("\f");
                }
        }
        return i;
    }
    
    /**
     * takes an input of a date an passes it on
     */
    public String getDateOfLoan()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write date of loan created: ");
        String dateOfLoan = keyboard.nextLine();
        return dateOfLoan; 
    }
    
    /**
     * 
     */
    public String getDateOfReturn()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write date of loan to be returned: ");
        String dateOfReturn = keyboard.nextLine();
        return dateOfReturn; 
    }
    
    /**
     * takes an input of an id and returns information about a lendings instance 
     */
    public void showLoanInformation()
    {
        System.out.println("\f You have chosen to show the information about a person.");
        long id = getId();
        if(id!=0){
            lendingsCtr.getLending(id);
        }
    }
    
    /**
     * creates a new lend in a container with input of a person, a dvd, a initial date and return date.
     */
    public void createLend()
    {
        System.out.println("\f You have chosen to create a new lend");
        System.out.println(" who is the loaner?"); 
        long idLoaner = addrUI.getId();
        if(idLoaner < 1)
            {
                System.out.println("\fOperation aborted.\n");
            }
        else 
            {
            long idDvd = dvdUI.getId(); 
             if(idDvd < 1)   
             {
                System.out.println("\fOperation aborted.\n");
            }
            else 
            {
                String dateOfReturn = getDateOfReturn();
                String dateOfLoan = getDateOfLoan();
                lendingsCtr.createLend(idLoaner, idDvd, dateOfReturn, dateOfLoan);
                System.out.print("\fLoan has been registrered.\n ");
            }
       
      }
    }
    
    /**
     * prints a list of all lendings
     */
    private void listAllLendings()
    {
        System.out.println("\fList of all loans: ");
        System.out.println("----------------------");
        lendingsCtr.listAllLendings();
        System.out.println("----------------------");
        System.out.print("\n");
    }
    
    /**
     * returns a loan
     */
    private void returnLending()
    {
       System.out.println("\f You have chosen to return a loan.");
        long id = getId();
       lendingsCtr.returnDVDLendingId(id);
    }
    
     /**
    * prints a list of all persons registered in the system
    */
    private void listAllPersons()
    {
        addrUI.listAllPersons();
    }
    
     /**
    * prints a list of all dvds registered in the system
    */
    private void listAllDVDs()
    {
        dvdUI.listAllDvds();
    }
    
     /**
     * returns an error message in case of invalidt input
     */ 
    private void printNumError()
    {
        System.out.println("\f Invalid input, it has to be a number. \n");
    }
   }

